<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	cfc('core')->checklogin();
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('qq','nickname','avatar','birth','pcd','mobile','realmobile','diyformdata'));
	$item=$this->get('member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if(!$item){
		exi('找不到会员信息！','error');
	}
	$item['nickname']=$member['nickname']; 
	$item['avatar']=$member['avatar']; 
	$item['mobile']=$item['mobile']?:$member['mobile']; 
	$item['oldmobile']=$item['mobile'];
	$setting=uni_setting('register');
	$item['login_type']=$setting['login_type'];
	$return['item']=$return['post']=$item;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if($postdata['login_type']>0 && $postdata['oldmobile']!=$postdata['mobile'] && !check_sms_code($postdata['smscode'])){
		$oldmobile=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'mobile');
		if($postdata['mobile']!=$oldmobile){
			exi('手机验证码错误！不能修改手机！','error');
		}
	}elseif($postdata['login_type']>0 && $postdata['oldmobile']!=$postdata['mobile']){
		$realmobile=true;
		$postdata['realmobile']=$postdata['mobile'];
	}
	if(strlen($postdata['mobile'])!=11 || $postdata['mobile'][0]!='1' || !is_numeric($postdata['mobile'])){
			exi('手机号码格式错误，请输入11位，1开头的手机号！！','error');
	}
	$uid=pdo_getcolumn('core_members',array('mobile'=>trim($postdata['mobile']),'uniacid'=>$_SESSION['uniacid'],'id <>'=>$_SESSION['uid']),'id');
	if($uid && $postdata['mobile']!=$postdata['oldmobile']){
		exi('手机号已存在，请更换!','error');
	}
	if(!$postdata['name']){
		exi('请先完善姓名！','error');
	}
	if($postdata['code']){
	    $checkcode=$this->model('model')->checkcode($postdata['code']);
        if(!$checkcode) {
            exi('请填写正确的身份证!', 'error');
        }
    }
	$update=array(
		'name'=>($postdata['name']),
		'code'=>($postdata['code']),
		'mobile'=>($postdata['mobile']),
		'danwei'=>($postdata['danwei']),
	);
	$this->update('member',$update,array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	pdo_update('core_members',array('mobile'=>$postdata['mobile']),array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($postdata['nickname']){
		$_SESSION['member']['nickname']=$postdata['nickname'];
	}
	$oldmobile=$oldmobile?:$postdata['oldmobile'];
	if($oldmobile!=$postdata['mobile'] && $realmobile){
		$mid=model('core/member')->merge_memberdata($postdata['mobile'],'',$oldmobile);
		if($_SESSION['uid']!=$mid){
			model('core/member')->login($mid);
			exi('设置成功！登录已绑定本手机的会员！','','/myhome/');
		}
	}
	exi('设置成功！','','/myhome/');
}
if($_RQ['op']=='setmobile'){
	//获取手机号
	$post=get_postdata();
	$params=array('openid'=>$_SESSION['openid'],'uniacid'=>$_SESSION['uniacid']);
	if($_SESSION['container']=='ttapp'){
		$params['ttgid']=$_SESSION['appgid'];
	}elseif($_SESSION['container']=='aliapp'){
		$params['aligid']=$_SESSION['appgid'];
	}elseif($_SESSION['container']=='baiduapp'){
		$params['baidugid']=$_SESSION['appgid'];
	}else{
		$params['wxgid']=$_SESSION['appgid'];
	}
	$uid=pdo_getcolumn('core_fans',$params,'mid');
	if($uid){
		$mid=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$uid),'id');
	}
	//解密获取手机号		
	require ROOT_D.'/core/library/wxapp_decode/wxBizDataCrypt.php';
	if($_SESSION['container']=='ttapp'){
			$appidname='tt_appid';
	}elseif($_SESSION['container']=='aliapp'){
			$appidname='ali_appid';
	}elseif($_SESSION['container']=='baiduapp'){
			$appidname='baidu_appid';
	}else{
			$appidname='appid';
	}
	$appid=pdo_getcolumn('core_account_wxapp',array('id'=>$_SESSION['appgid'],'uniacid'=>$_SESSION['uniacid']),$appidname);
	if(!$appid || !$_SESSION['session_key']){
			exi('小程序参数错误！无法获取粉丝信息！','error');
	}
	$pc = new WXBizDataCrypt($appid, $_SESSION['session_key']);
	$errCode = $pc->decryptData($post['encryptedData'], $post['iv'],$data);
	if ($errCode) {
			exi($errCode ,'error');
	}
	$data=json_decode($data,true);
	$mobile=$data['purePhoneNumber'];//获取没有区的手机号
	if(!$uid){
			//尚未注册，直接注册			
			$new_member=array(
				'mobile'=>$mobile,
				'realmobile'=>$mobile
			);
			$uid=model('core/member')->register($new_member);
			model('core/member')->login($uid);
			pdo_update('core_fans',array('mid'=>$uid),$params);
	}else{
			$oldmobile=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$uid),'mobile');
			if($mobile!=$oldmobile){
				pdo_update('core_members',array('mobile'=>$mobile,'realmobile'=>$mobile),array('uniacid'=>$_SESSION['uniacid'],'id'=>$uid));
				$mid=model('core/member')->merge_memberdata($mobile);
				if($_SESSION['uid']!=$mid){
					model('core/member')->login($mid);
				}
			}
	}
	$res=array('uid'=>$uid,'mobile'=>$mobile);
	exi($res);
}
if($_RQ['op']=='getdata'){
	$register=uni_setting('register');
	$register['user_xieyi']=$register['user_xieyi']?:'本平台尊重和保护用户的隐私。本隐私政策将告诉您我们如何收集和使用有关您的信息，以及我们如何保护这些信息的安全。您成为本平台用户前务必仔细阅读本隐私条款并同意所有隐私条款。本隐私政策条款在您注册成为本平台用户后立即生效，并对您及本平台产生约束力。';
	$return['register']=$register;
	exi($return);
}